﻿Imports WindowsApplication1.GoogleAnalyticsMailHandler
Imports WindowsApplication1.TestNotifyWindow

Public Delegate Sub NotifyDelegate(ByVal emails As List(Of Email))
Public Delegate Sub ErrorDelegate(ByVal errorObject As Exception)

Public Class MainAppForm

    Dim m_emailListener As EmailListener
    Dim m_emailViewer As EmailViewer

    Public Sub New()
        m_emailViewer = New EmailViewer()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "Start Listening" Then
            Dim portNumber As Integer
            If String.IsNullOrEmpty(txtHost.Text) Then
                MessageBox.Show("Host Name is required")
            End If
            If String.IsNullOrEmpty(txtPort.Text) Then
                MessageBox.Show("Port Number is required")
            End If
            If Not Integer.TryParse(txtPort.Text, portNumber) Then
                MessageBox.Show("Port Number should be integer value")
            End If
            If String.IsNullOrEmpty(txtEmail.Text) Then
                MessageBox.Show("Email address is required")
            End If
            If String.IsNullOrEmpty(txtPassword.Text) Then
                MessageBox.Show("Password is required")
            End If

            m_emailListener = New EmailListener( _
                    txtHost.Text, _
                    portNumber, _
                    txtEmail.Text, _
                    txtPassword.Text, _
                    chkSecuredConnection.Checked, _
                    numPeriod.Value)

            AddHandler m_emailListener.EmailsRecived, AddressOf m_emailListener_EmailsRecived
            AddHandler m_emailListener.ErrorOccured, AddressOf m_emailListener_ErrorOccured
            m_emailListener.StartListening()
            SetControlsState(False)
            Button1.Text = "Stop Listening"

        ElseIf Not m_emailListener Is Nothing Then
            m_emailListener.StopListening()
            SetControlsState(True)
            Button1.Text = "Start Listening"
        End If


    End Sub

    Private Sub SetControlsState(ByVal isEnabled As Boolean)
        txtEmail.Enabled = isEnabled
        txtPassword.Enabled = isEnabled
        txtPort.Enabled = isEnabled
        txtHost.Enabled = isEnabled
        numPeriod.Enabled = isEnabled
        chkSecuredConnection.Enabled = isEnabled

    End Sub

    Private Sub m_emailListener_ErrorOccured(ByVal sender As Object, ByVal errorObject As Exception)
        Dim args = New List(Of Object)
        args.Add(errorObject)

        If Not Me.IsDisposed Then
            Me.BeginInvoke(New ErrorDelegate(AddressOf ShowErrorMessage), args.ToArray())

        End If
    End Sub

    Private Sub m_emailListener_EmailsRecived(ByVal sender As Object, ByVal emails As List(Of Email))

        Dim args = New List(Of Object)
        args.Add(emails)

        If Not Me.IsDisposed Then
            Me.BeginInvoke(New NotifyDelegate(AddressOf ShowNotifyWindow), args.ToArray())

        End If
    End Sub

    Private Sub ShowNotifyWindow(ByVal emails As List(Of Email))
        Dim nw As NotifyWindow
        m_emailViewer.AddEmails(emails)
        nw = New NotifyWindow(String.Format("You have {0} new message{1}", _
                                             emails.Count, _
                                             IIf(emails.Count = 1, "", "s")))
        AddHandler nw.TextClicked, AddressOf TextClick

        nw.SetDimensions(170, 80)
        nw.Notify()
    End Sub

    Private Sub ShowErrorMessage(ByVal errorObject As Exception)
        MessageBox.Show(errorObject.Message, "Email Client", MessageBoxButtons.OK, MessageBoxIcon.Error)
        m_emailListener.StopListening()
        SetControlsState(True)
        Button1.Text = "Start Listening"

    End Sub


    Private Sub TextClick(ByVal sender As Object, ByVal e As EventArgs)
        m_emailViewer.Show()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Not m_emailListener Is Nothing Then
            m_emailListener.StopListening()
        End If
        Application.Exit()
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        m_emailViewer.Show()
    End Sub

End Class